Pablo Leon-Rodenas |>
pablo.leonrodenas@nhs.net
Analytics Analyst | Analytical Products team
2 Aug 2021
Aim
This presentation describes
how to build
and publish a shiny
dashboard Using daily data
from JHU CSSE Github repository.
This is the data repository for the 2019 Novel Coronavirus Visual Dashboard operated by the Johns Hopkins University Center for Systems Science and Engineering (JHU CSSE). Also, Supported by ESRI Living Atlas Team and the Johns Hopkins University Applied Physics Lab (JHU APL).
List of topics Today we will cover these four topics:
1. Basic Shiny app overview
2. Getting data from GitHub
3. Build visualizations that include animations (maps and plots)
4. Dynamic Data tables (allowing selection and filtering of data in those tables)
5. Interactive plots using plotly libraries in Python and R
6. Predictive modelling as a Shiny app module
7. Publishing a Shiny app in R Studio server and GitHub.
Shiny is an R package that makes it easy to build interactive web applications (apps) in R. Once a shiny app is designed and deployed, it can be launched locally from any internet browser or hosted in the Shiny server to be accessed 24/7.
This is an example of a Shiny app where by selecting a specific value in the top slider causes the bar chart bins to change accordingly
How it started This basic dashboard has two main components a side bar and a plot
How is it going From this basic dashboard we can expand it to include:
Dashboard with Maps and tables
All the plots are interactive allowing user to use tooltips and zoom in or zoom out. Also download image as png.
Dashboard with plots and tabs
Dashboard with Maps and tables
Interactive plotly line charts tabset layout
Dashboard with Maps and tables
| Feature | Function | Code |
|---|---|---|
| KPI | infoBoxOutput() | fluidRow( |
| infoBoxOutput(“Totalrecovered”, width = 3), | ||
| infoBoxOutput(“Totaldeaths”, width = 3), | ||
| infoBoxOutput(“Date”, width = 3) | ||
| ), | ||
| MAPS | infomap | dataframe %>% |
| leaflet() %>% | ||
| addTiles() %>% | ||
| setView(lng = -10, lat = 20, zoom = 3) %>% | ||
| addCircles(lng = ~ Long, | ||
| lat = ~Lat, | ||
| weight = 5, | ||
| radius = ~sqrt(dataframe$Deaths)*1000, | ||
| Feature | Function | Code |
|---|---|---|
| MAPS | infomap | dataframe %>% |
| popup = paste0( | ||
| “Country: ”,dataframe$Country,'',dataframe$date, | ||
| “ Province: ”,dataframe$Province, |
||
| “ Confirmed=”,dataframe$Confirmed, |
||
| “ Deaths=”,dataframe$Deaths, |
||
| “ Recovered=”,dataframe$Recovered, |
||
| sep = “ ” | ||
| ), | ||
| fillColor = “lightblue”, | ||
| highlightOptions = highlightOptions( weight = 10, color = “red”, fillColor = “green”) | ||
| ) %>% | ||
| addLegend( pal = pal_sb, values = dataframe$Deaths, | ||
| position = “bottomleft”, | ||
| title = “Total COVID19 deaths”)}) |
The data source to produce this dashboard is the following Github repository
https://github.com/CSSEGISandData/COVID-19
| Function | Function | Code |
|---|---|---|
| Get data daily update | downlopad data from Github | dataframe %>% |
| DownloadTheCOVIDData <- function() {} | ||
| Update daily data | Check data file for latest download time, it greater than 24h download again min |
Script to download data and update daily information for the app
| Function | Function | Code |
|---|---|---|
| Get daily data | download it from Github | dataframe %>% |
| DownloadTheCOVIDData <- function() { | ||
| if(!dir.exists(“data”)){ | ||
| dir.create(“data”) | ||
| download.file( | ||
| url = “https://github.com/CSSEGISandData/COVID-19/archive/master.zip”, | ||
| destfile = “data/covid19JH.zip” ) | ||
| datapath <- “COVID-19-master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19” | ||
| We need to unzip them and get the three .csv files | ||
| unzip(zipfile = “data/covid19JH.zip”, | ||
| files = paste0(data_path, c(“confirmed_global.csv”, | ||
| “deaths_global.csv”, | ||
| “recovered_global.csv”)),exdir = “data”, junkpaths = T)} |
Includes next month Covid19 cases forecast and RMSE accuracy measure to assess model performance.
Uses two packages: Tidymodels and Modeltime
1. Tidymodels The tidymodels framework is a collection of packages for modeling and machine learning using tidyverse principles. Tidymodels website
2. Modeltime
The tidymodels extension for Time Series Modeling
Modeltime website
Forecast example using the above package. Models: ARIMA, PROPHET, ETS, SARIMA
Tidymodels allows you to implement a full cycle of models creation and validation
It is a framework designed to:Create and Tune robust models
Compare and work with your models,Develop custom modeling tools
Modeltime is designed to combine different time series analysis
ARIMA, ETS, Exponential Smoothing
- Also includes PROPHET Facebook model
- Streamline models creation
- It is part an entire modelling ecosystem
In terms of security, we need to avoid SQL injections when creating Shiny forms to enter data We want to avoid users to execute in thb DBMS without having legitimate access to it:
dbGetQuery(conn,paste0(“SELECT * FROM City LIMIT”,input$nrows,“;”))
input$rows has been put directly into the query
We can avoid such attack by converting first the query into an integer, losing its meaning as a command to the database.
Solution: dbGetQuery(conn,paste0(“SELECT * FROM City LIMIT”,input$nrows,“;”, as.integer(input$nrows)[1],“;”))
This article will show you how to create a shinyapps.io account and deploy your first application to the cloud
Thank you all for your time
Any questions?
Contat for further details about Shiny:pablo.leonrodenas@nhs.net